package cn.b.sky.mes.cm.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkDayPeriod;
import cn.b.sky.mes.cm.model.WorkDayType;
import cn.b.sky.mes.cm.model.condition.WorkDayTypeCondition;
import cn.b.sky.mes.cm.service.WorkDayTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/workDayType/*")
public class WorkDayTypeController extends SecurityController {
  @Resource
  WorkDayTypeService workDayTypeService;

  @RequestMapping("workDayTypeList")
  public String workDayTypeList(Model model, @ModelAttribute("condition") WorkDayTypeCondition workDayTypeCondition,
      String navTabId) {
    workDayTypeCondition.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(workDayTypeCondition.getOrderField())) {
      workDayTypeCondition.setOrderField("createDateTime");
    }
    List<WorkDayType> workDayTypeList = workDayTypeService.selectWorkDayTypeByCondition(workDayTypeCondition);
    int count = workDayTypeService.selectWorkDayTypeCount(workDayTypeCondition);
    workDayTypeCondition.setData(workDayTypeList);
    workDayTypeCondition.setTotalCount(count);
    model.addAttribute("page", workDayTypeCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/workDayType/workDayType_list";
  }

  @RequestMapping("validateWorkDayType")
  @ResponseBody
  public boolean validateWorkDayType(String dayTypeCode, Model model) {
    if (!StringUtils.isBlank(dayTypeCode)) {
      return null == workDayTypeService.selectWorkDayType(getSiteCode(), dayTypeCode);
    }
    return true;

  }

  @RequestMapping("addWorkDayType")
  public String addWorkDayType(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "cm/workDayType/workDayType_add";
  }

  @RequestMapping("editWorkDayType")
  public String editWorkDayType(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    WorkDayType workDayType = workDayTypeService.selectWorkDayType(getSiteCode(), id);
    List<WorkDayPeriod> periods = workDayTypeService.selectWorkDayPeriodByWorkDay(getSiteCode(), id);
    workDayType.setPeriods(periods);
    model.addAttribute("workDayType", workDayType);
    return "cm/workDayType/workDayType_add";
  }

  @RequestMapping("saveWorkDayType")
  @ResponseBody
  public DWZResult saveWorkDayType(Model model, String id, WorkDayType workDayType, HttpServletRequest request) {
    if (workDayType == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    workDayType.setSiteCode(getSiteCode());
    List<WorkDayPeriod> pers = workDayType.getPeriods();
    Iterator<WorkDayPeriod> iter = pers.iterator();
    while (iter.hasNext()) {

      WorkDayPeriod wd = iter.next();
      if (StringUtils.isEmpty(wd.getStartTime())) {
        iter.remove();
      } else {
        if (wd.getSpanDaysFlag() == 2) {
          wd.setSpanDaysFlag(0);
          wd.setNextDayFlag(1);
        }
      }
    }
    setCreateInfo(workDayType);
    if (StringUtils.isEmpty(id)) {
      workDayTypeService.insertWorkDayType(workDayType);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    } else {
      workDayTypeService.updateWorkDayType(workDayType);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);

    }

  }

  @RequestMapping("viewMaterial")
  public String viewMaterial(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    WorkDayType workDayType = workDayTypeService.selectWorkDayType(getSiteCode(), id);
    List<WorkDayPeriod> periods = workDayTypeService.selectWorkDayPeriodByWorkDay(getSiteCode(), id);
    workDayType.setPeriods(periods);
    model.addAttribute("workDayType", workDayType);
    return "cm/workDayType/workDayType_view";
  }

  @RequestMapping("deleteWorkDayType")
  @ResponseBody
  public DWZResult deleteWorkDayType(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = workDayTypeService.deleteWorkDayType(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }
}
